﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TraCuu_Update
{
    public partial class PhieuThuGUI : UserControl, GUI.UctrlGUI
    {
        class cbxItem
        {
            public int _ID;
            public int _IDKhachHang;
            public String _Text;
            public float _SoTien;

            public cbxItem(int id, int idKhachHang, float sotien, String text)
            {
                _ID = id;
                _Text = text;
                _IDKhachHang = idKhachHang;
                _SoTien = sotien;
            }

            public override string ToString()
            {
                return _Text;
            }
        }

        BLL.DanhSachPhieuThuTienBLL _DanhSachPhieuBLL;
        BLL.DanhSachKhachHangBLL _DanhSachKhachHangBLL;

        public PhieuThuGUI()
        {
            InitializeComponent();
            _DanhSachPhieuBLL = new BLL.DanhSachPhieuThuTienBLL();
            _DanhSachKhachHangBLL = new BLL.DanhSachKhachHangBLL();
        }

        public String getName()
        {
            return "TRA CỨU PHIẾU THU TIỀN";
        }

        private void PhieuThuGUI_Load(object sender, EventArgs e)
        {
            ResetGui();
            UpdateCBMaPhieu();
            if (cbx_MaPhieu.Items.Count > 0)
                cbx_MaPhieu.SelectedIndex = 0;
        }

        private void btn_Tim_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("TIM");
        }

        private void dt_NgayThu_ValueChanged(object sender, EventArgs e)
        {
            UpdateCBMaPhieu();
            if (cbx_MaPhieu.Items.Count > 0)
                cbx_MaPhieu.SelectedIndex = 0;
            else
            {
                ResetGui();
            }
        }

        private void cbx_MaPhieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateKhacHangInfo();
        }

        private void ResetGui()
        {
            txt_DiaChi.Text = String.Empty;
            txt_DienThoai.Text = String.Empty;
            txt_Email.Text = String.Empty;
            txt_HoTen.Text = String.Empty;
            txt_SoTien.Text = String.Empty;
        }

        private void UpdateCBMaPhieu()
        {
            cbx_MaPhieu.Items.Clear();
            List<DTO.PhieuThuDTO> phieu = 
                _DanhSachPhieuBLL.GetPhieuThuByNgayThu(dt_NgayThu.Value);
            foreach (DTO.PhieuThuDTO dto in phieu)
            {
                cbx_MaPhieu.Items.Add(new cbxItem(dto.ID, dto.IDKhachHang, dto.SoTien,
                    "Phiếu Thu " + dto.ID.ToString()));
            }
        }

        private void UpdateKhacHangInfo()
        {
            ResetGui();
            DTO.KhachHangDTO dto = _DanhSachKhachHangBLL.GetKhachHangByID(
                (cbx_MaPhieu.SelectedItem as cbxItem)._IDKhachHang
                );
            txt_DiaChi.Text = dto.DiaChi;
            txt_DienThoai.Text = dto.DienThoai;
            txt_Email.Text = dto.Email;
            txt_HoTen.Text = dto.HoTen;

            txt_SoTien.Text = (cbx_MaPhieu.SelectedItem as cbxItem)._SoTien.ToString();
        }
    }
}
